﻿using System;
using GalaSoft.MvvmLight.Command;
using Microsoft.Phone.Tasks;
using WP7Contrib.Logging;
using WP7Contrib.Services.Navigation;
using WP7Contrib.Services.Storage;

namespace dIRca.WP7.ViewModels
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class AboutViewModel : ViewModelBaseWp7
    {
        #region Commands
        private bool _canGoToProjectUrl;
        public bool CanGoToProjectUrl
        {
            get { return _canGoToProjectUrl; }
            set
            {
                if (_canGoToProjectUrl == value)
                    return;

                var oldValue = _canGoToProjectUrl;
                _canGoToProjectUrl = value;
                GoToProjectUrlCommand.RaiseCanExecuteChanged();
                RaisePropertyChanged("CanGoToProjectUrl", oldValue, CanGoToProjectUrl, true);
            }
        }

        RelayCommand _goToProjectUrlCommand;
        public RelayCommand GoToProjectUrlCommand
        {
            get
            {
                return _goToProjectUrlCommand ?? (_goToProjectUrlCommand =
                    new RelayCommand(() => this.GoToUrl(ProjectUrl), () => this.CanGoToProjectUrl));
            }
        }

        private bool _canGoToTwitterUrl;
        public bool CanGoToTwitterUrl
        {
            get { return _canGoToTwitterUrl; }
            set
            {
                if (_canGoToTwitterUrl == value)
                    return;

                var oldValue = _canGoToTwitterUrl;
                _canGoToTwitterUrl = value;
                GoToTwitterUrlCommand.RaiseCanExecuteChanged();
                RaisePropertyChanged("CanGoToTwitterUrl", oldValue, CanGoToTwitterUrl, true);
            }
        }

        RelayCommand _goToTwitterUrlCommand;
        public RelayCommand GoToTwitterUrlCommand
        {
            get
            {
                return _goToTwitterUrlCommand ?? (_goToTwitterUrlCommand =
                    new RelayCommand(() => this.GoToUrl(TwitterUrl), () => this.CanGoToTwitterUrl));
            }
        }
        #endregion

        #region Properties
        private string _pageTitle;
        public string PageTitle
        {
            get { return _pageTitle; }
            set
            {
                if (_pageTitle == value)
                    return;

                var oldValue = _pageTitle;
                _pageTitle = value;
                RaisePropertyChanged("PageTitle", oldValue, PageTitle, true);
            }
        }

        private string _author;
        public string Author
        {
            get { return _author; }
            set
            {
                if (_author == value)
                    return;

                var oldValue = _author;
                _author = value;
                RaisePropertyChanged("Author", oldValue, Author, true);
            }
        }

        private string _version;
        public string Version
        {
            get { return _version; }
            set
            {
                if (_version == value)
                    return;

                var oldValue = _version;
                _version = value;
                RaisePropertyChanged("Version", oldValue, Version, true);
            }
        }

        private Uri _projectUrl;
        public Uri ProjectUrl
        {
            get { return _projectUrl; }
            set
            {
                if (_projectUrl == value)
                    return;

                var oldValue = _projectUrl;
                _projectUrl = value;
                RaisePropertyChanged("ProjectUrl", oldValue, ProjectUrl, true);
            }
        }

        private Uri _twitterUrl;
        public Uri TwitterUrl
        {
            get { return _twitterUrl; }
            set
            {
                if (_twitterUrl == value)
                    return;

                var oldValue = _twitterUrl;
                _twitterUrl = value;
                RaisePropertyChanged("TwitterUrl", oldValue, TwitterUrl, true);
            }
        }
        
        private string _twitterUrlName;
        public string TwitterUrlName
        {
            get { return _twitterUrlName; }
            set
            {
                if (_twitterUrlName == value)
                    return;

                var oldValue = _twitterUrlName;
                _twitterUrlName = value;
                RaisePropertyChanged("TwitterUrlName", oldValue, TwitterUrlName, true);
            }
        }
        #endregion

        /// <summary>
        /// Initializes a new instance of the AboutViewModel class.
        /// </summary>
        public AboutViewModel(INavigationService navigationService, ILog log)
            : base(navigationService, log)
        {
            PageTitle = "About " + App.Title;
            Author = "Jeremy Brayton";
            Version = "0.1.0.0 Alpha Prime";
            ProjectUrl = new Uri(@"http://" + App.Title + ".codeplex.com");
            TwitterUrl = new Uri(@"http://twitter.com/#!/w0rddriven");
            TwitterUrlName = "@w0rddriven";
            CanGoToProjectUrl = true;
            CanGoToTwitterUrl = true;
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                // Code runs "for real": Connect to service, etc...
            }
        }

        void GoToUrl(Uri url)
        {
            CanGoToProjectUrl = false;
            CanGoToTwitterUrl = false;
            var webBrowserTask = new WebBrowserTask() { URL = url.ToString(), };
            webBrowserTask.Show();
        }

        public override void Cleanup()
        {
            // Clean own resources if needed

            base.Cleanup();
        }

        protected override void IsBeingActivated(IStorage storage)
        {
        }

        protected override void IsBeingDeactivated(IStorage storage)
        {
        }
    }
}